package com.glorysoft.tonglingxjpda.customview;

import android.Manifest;
import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatDialogFragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.glorysoft.tonglingxjpda.R;
import com.glorysoft.tonglingxjpda.utils.BaseConsumerPermission;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;
import com.tbruyelle.rxpermissions2.RxPermissions;

import java.io.File;

/**
 * @author huangxin
 * @date 2015/11/10
 * @desc 通用dialog
 */
public class SelectorPicDialog extends AppCompatDialogFragment {

    private View mView;
    public static final int REQUEST_CAMEAR = 100;
    public static final int REQUEST_VIDEO = 101;
    public static final int REQUEST_CODE_LOCAL = 106;
    private TextView tvTitle;
    private TextView tvVideo, tvCamera, tvAlbum, tvCancel;
    public static boolean mSelectVideo = false;
    private boolean mRecordVideo = false;
    public static String fileFolderPath;
    private String title;
    public static final String TYPE_CAMERA = "camera";
    public static final String TYPE_ALBUM = "album";
    public static final String TYPE_VIDEO = "video";

    @Override
    public void show(FragmentManager manager, String tag) {
        if (!isAdded()) {
            FragmentTransaction ft = manager.beginTransaction();
            ft.add(this, tag);
            ft.commitAllowingStateLoss();
        }

    }
    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        if (dialog != null) {
            dialog.getWindow().setLayout(FreeApi_StaticMembers.SCREEN_WIDTH, ViewGroup.LayoutParams.WRAP_CONTENT);
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getDialog().setCanceledOnTouchOutside(true);//设置外部点击取消
        Window dialogWindow = getDialog().getWindow();
        dialogWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        dialogWindow.setWindowAnimations(R.style.DialogAnimation); // 添加动画
        dialogWindow.setGravity(Gravity.BOTTOM);
        mView = inflater.inflate(R.layout.dialog_select_picture, null);
        if (isAndroidQ()) {
            fileFolderPath = getActivity().getExternalFilesDir(Environment.DIRECTORY_PICTURES) + File.separator + "HiWork" + File.separator;
        } else {
            fileFolderPath = FreeApi_StaticMembers.ROOT_PATH + "/" + FreeApi_StaticMembers.DOWN_PATH;
        }
        initView();
        return mView;
    }

    public static boolean isAndroidQ() {
        if (Build.VERSION.SDK_INT >= 29) {
            return true;
        } else {
            return false;
        }
    }

    private void initView() {
        tvAlbum = mView.findViewById(R.id.tv_album);
        tvCamera = mView.findViewById(R.id.tv_camera);
        tvCancel = mView.findViewById(R.id.tv_cancel);
        tvTitle = mView.findViewById(R.id.tv_title);
        tvVideo = mView.findViewById(R.id.tv_video);
        tvTitle.setText(title);
        if (mRecordVideo) {
            tvVideo.setVisibility(View.VISIBLE);
            tvCamera.setVisibility(View.GONE);
        } else {
            tvVideo.setVisibility(View.GONE);
            tvCamera.setVisibility(View.VISIBLE);
        }
        tvAlbum.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
                new RxPermissions(getActivity())
                        .requestEach(Manifest.permission.READ_EXTERNAL_STORAGE)
                        .subscribe(new BaseConsumerPermission() {
                            @Override
                            public void OnSuccess() {
                                if (mCameraAndAlbumListener != null) {
                                    mCameraAndAlbumListener.OnSuccess(TYPE_ALBUM);
                                }


                            }

                            @Override
                            public void OnFailed() {
                                if (mCameraAndAlbumListener != null) {
                                    mCameraAndAlbumListener.OnFailed(TYPE_ALBUM);
                                }
                            }

                            @Override
                            public void OnFailedNotAsking() {
                                if (mCameraAndAlbumListener != null) {
                                    mCameraAndAlbumListener.OnFailedNotAsking(TYPE_ALBUM);
                                }
                            }
                        });
            }

        });

        tvCamera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
                new RxPermissions(getActivity())
                        .requestEachCombined(Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                        .subscribe(new BaseConsumerPermission() {
                            @Override
                            public void OnSuccess() {
                                if (mCameraAndAlbumListener != null) {
                                    mCameraAndAlbumListener.OnSuccess(TYPE_CAMERA);
                                }

                            }

                            @Override
                            public void OnFailed() {
                                if (mCameraAndAlbumListener != null) {
                                    mCameraAndAlbumListener.OnFailed(TYPE_CAMERA);
                                }
                            }

                            @Override
                            public void OnFailedNotAsking() {
                                if (mCameraAndAlbumListener != null) {
                                    mCameraAndAlbumListener.OnFailedNotAsking(TYPE_CAMERA);
                                }
                            }
                        });
            }
        });
        tvVideo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
                new RxPermissions(getActivity())
                        .requestEachCombined(Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.RECORD_AUDIO)
                        .subscribe(new BaseConsumerPermission() {
                            @Override
                            public void OnSuccess() {
                                if (mCameraAndAlbumListener != null) {
                                    mCameraAndAlbumListener.OnSuccess(TYPE_VIDEO);
                                }

                            }

                            @Override
                            public void OnFailed() {
                                if (mCameraAndAlbumListener != null) {
                                    mCameraAndAlbumListener.OnFailed(TYPE_VIDEO);
                                }
                            }

                            @Override
                            public void OnFailedNotAsking() {
                                if (mCameraAndAlbumListener != null) {
                                    mCameraAndAlbumListener.OnFailedNotAsking(TYPE_VIDEO);
                                }
                            }
                        });

            }
        });
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });

    }

    public void setSelectVideo(boolean selectVideo) {
        this.mSelectVideo = selectVideo;
    }

    public void setRecordVideo(boolean recordVideo) {
        this.mRecordVideo = recordVideo;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    private CameraAndAlbumListener mCameraAndAlbumListener;

    public void setOnCameraAndAlbumListener(CameraAndAlbumListener cameraAndAlbumListener) {
        this.mCameraAndAlbumListener = cameraAndAlbumListener;
    }

    public interface CameraAndAlbumListener {
        void OnSuccess(String type);

        void OnFailed(String type);

        void OnFailedNotAsking(String type);
    }
}
